//
//  AppDelegate.swift
//  新浪微博
//
//  Created by fanhoucheng on 16/9/22.
//  Copyright © 2016年 fanhoucheng. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        //1.创建视图
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        //注册通知
        regisiter()
        //2.设置视图根控制器
        //window?.rootViewController = HCTarBarController()
        //window?.rootViewController = HCWelcomeViewController()
        window?.rootViewController = HCUserAccountViewModel.sharedUserAccountViewModel.userLogin ? HCWelcomeViewController() : HCTarBarController()
        //3.让视图可视化
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //注册通知
    func regisiter() {
        NotificationCenter.default.addObserver(self, selector: #selector(setRootViewController(notification:)), name: NSNotification.Name(rawValue: kChnageRootViewController), object: nil)
    }
    
    //通知的响应方法
    @objc private func setRootViewController(notification: Notification) {
        if notification.object == nil {//如果不为nil就是
            window?.rootViewController = HCTarBarController()
        } else {
            window?.rootViewController = HCWelcomeViewController()
        }
    }
    
    deinit {
        //销毁通知
        NotificationCenter.default.removeObserver(self)
    }

}

